package com.manage.project.biz.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringRegularCheck;
import com.manage.project.biz.domain.CivilEnginneeringTechnicalTotal;
import com.manage.project.biz.service.IBizGongluService;
import com.manage.project.biz.service.IBizSuidaoService;
import com.manage.project.biz.service.ICivilEnginneeringRegularCheckService;
import com.manage.project.biz.service.ICivilEnginneeringTechnicalTotalService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 土建结构技术状况评定Controller
 *
 * @author 小赵
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/civil/total")
public class CivilEnginneeringTechnicalTotalController extends BaseController
{
    @Autowired
    private ICivilEnginneeringTechnicalTotalService civilEnginneeringTechnicalTotalService;
    @Autowired
    private ICivilEnginneeringRegularCheckService civilEnginneeringRegularCheckService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;

    /**
     * 查询土建结构技术状况评定列表
     */
    @PreAuthorize("@ss.hasPermi('civil:total:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal)
    {
        startPage();
        List<CivilEnginneeringTechnicalTotal> list = civilEnginneeringTechnicalTotalService.selectCivilEnginneeringTechnicalTotalList(civilEnginneeringTechnicalTotal);
        return getDataTable(list);
    }

    /**
     * 导出土建结构技术状况评定列表
     */
    @PreAuthorize("@ss.hasPermi('civil:total:export')")
    @Log(title = "土建结构技术状况评定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export( CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal) throws IOException {
        List<CivilEnginneeringTechnicalTotal> list = civilEnginneeringTechnicalTotalService.selectCivilEnginneeringTechnicalTotalList(civilEnginneeringTechnicalTotal);
        ExcelUtil<CivilEnginneeringTechnicalTotal> util = new ExcelUtil<CivilEnginneeringTechnicalTotal>(CivilEnginneeringTechnicalTotal.class);
        return util.exportExcel( list,"土建结构技术状况评定数据");
    }

    /**
     * 获取土建结构技术状况评定详细信息
     */
    @PreAuthorize("@ss.hasPermi('civil:total:query')")
    @GetMapping(value = "/{technologyId}")
    public AjaxResult getInfo(@PathVariable("technologyId") Long technologyId)
    {
        return AjaxResult.success(civilEnginneeringTechnicalTotalService.selectCivilEnginneeringTechnicalTotalByTechnologyId(technologyId));
    }

    /**
     * 新增土建结构技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('civil:total:add')")
    @Log(title = "土建结构技术状况评定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal) {
        //获取tunnelName参数
        String tunnelName = civilEnginneeringTechnicalTotal.getTunnelName();
        String roadName=civilEnginneeringTechnicalTotal.getRoadName();
        String tunnelId=civilEnginneeringTechnicalTotal.getTunnelId();
        Date inspectionDate=civilEnginneeringTechnicalTotal.getAdjustData();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        List<CivilEnginneeringRegularCheck> list1=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"洞口",inspectionDate);
        if(list1.size()<1){
            return new AjaxResult().error("该隧道还未进行洞口的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list2=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"洞门",inspectionDate);
        if(list2.size()<1){
            return new AjaxResult().error("该隧道还未进行洞门的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list3=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"装饰物",inspectionDate);
        if(list3.size()<1){
            return new AjaxResult().error("该隧道还未进行装饰物的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list4=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"吊顶及预埋件",inspectionDate);
        if(list4.size()<1){
            return new AjaxResult().error("该隧道还未进行吊顶及预埋件的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list5=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"衬砌",inspectionDate);
        if(list5.size()<1){
            return new AjaxResult().error("该隧道还未进行衬砌的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list6=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"路面",inspectionDate);
        if(list6.size()<1){
            return new AjaxResult().error("该隧道还未进行路面的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list7=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"检修道",inspectionDate);
        if(list7.size()<1){
            return new AjaxResult().error("该隧道还未进行检修道的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list8=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"排水设施",inspectionDate);
        if(list8.size()<1){
            return new AjaxResult().error("该隧道还未进行排水设施的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list9=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"交通标志/标线",inspectionDate);
        if(list9.size()<1){
            return new AjaxResult().error("该隧道还未进行交通标志/标线的定期检查，请先去填写定期检查记录表");
        }

        int direct = civilEnginneeringTechnicalTotal.getDirectLimit();//装饰物权重
        int top = civilEnginneeringTechnicalTotal.getTopLimit();//吊顶权重
        int door = civilEnginneeringTechnicalTotal.getTunnelDoorLimit();//洞门权重
        int hole = civilEnginneeringTechnicalTotal.getTunnelHoleLimit();//洞口权重
        int lining = civilEnginneeringTechnicalTotal.getLinningLimit();//衬砌权重
        int roadsurface = civilEnginneeringTechnicalTotal.getRoadsurfaceLimit();//路面权重
        int jianxiudao = civilEnginneeringTechnicalTotal.getJianxiudaoLimit();//检修道权重
        int water = civilEnginneeringTechnicalTotal.getWaterLimit();//排水设施权重
        int marking = civilEnginneeringTechnicalTotal.getMarkingLimit();//交通标志或标线权重
        if(roadInfoService.checkByRoadName(civilEnginneeringTechnicalTotal.getRoadName())==null){
            return new AjaxResult().error("当前公路不存在，请输入正确的公路名称");
        }
        if ((direct + top + hole + roadsurface + jianxiudao + lining + water + marking + door)!= 100) {
            return new AjaxResult().error("各项权重和必须为100，请检查后重新输入");
        } else {
            return toAjax(civilEnginneeringTechnicalTotalService.insertCivilEnginneeringTechnicalTotal(civilEnginneeringTechnicalTotal));
        }
    }

    /**
     * 修改土建结构技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('civil:total:edit')")
    @Log(title = "土建结构技术状况评定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal)
    {

        //获取tunnelName参数
        String tunnelName = civilEnginneeringTechnicalTotal.getTunnelName();
        String roadName=civilEnginneeringTechnicalTotal.getRoadName();
        String tunnelId=civilEnginneeringTechnicalTotal.getTunnelId();
        Date inspectionDate=civilEnginneeringTechnicalTotal.getAdjustData();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        List<CivilEnginneeringRegularCheck> list1=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"洞口",inspectionDate);
        if(list1.size()<1){
            return new AjaxResult().error("该隧道还未进行洞口的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list2=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"洞门",inspectionDate);
        if(list2.size()<1){
            return new AjaxResult().error("该隧道还未进行洞门的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list3=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"装饰物",inspectionDate);
        if(list3.size()<1){
            return new AjaxResult().error("该隧道还未进行装饰物的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list4=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"吊顶及预埋件",inspectionDate);
        if(list4.size()<1){
            return new AjaxResult().error("该隧道还未进行吊顶及预埋件的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list5=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"衬砌",inspectionDate);
        if(list5.size()<1){
            return new AjaxResult().error("该隧道还未进行衬砌的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list6=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"路面",inspectionDate);
        if(list6.size()<1){
            return new AjaxResult().error("该隧道还未进行路面的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list7=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"检修道",inspectionDate);
        if(list7.size()<1){
            return new AjaxResult().error("该隧道还未进行检修道的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list8=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"排水设施",inspectionDate);
        if(list8.size()<1){
            return new AjaxResult().error("该隧道还未进行排水设施的定期检查，请先去填写定期检查记录表");
        }
        List<CivilEnginneeringRegularCheck> list9=civilEnginneeringRegularCheckService.
                getByTunnelNameAndFacilityName(tunnelName,"交通标志/标线",inspectionDate);
        if(list9.size()<1){
            return new AjaxResult().error("该隧道还未进行交通标志/标线的定期检查，请先去填写定期检查记录表");
        }

        int direct = civilEnginneeringTechnicalTotal.getDirectLimit();//装饰物权重
        int top = civilEnginneeringTechnicalTotal.getTopLimit();//吊顶权重
        int door = civilEnginneeringTechnicalTotal.getTunnelDoorLimit();//洞门权重
        int hole = civilEnginneeringTechnicalTotal.getTunnelHoleLimit();//洞口权重
        int lining = civilEnginneeringTechnicalTotal.getLinningLimit();//衬砌权重
        int roadsurface = civilEnginneeringTechnicalTotal.getRoadsurfaceLimit();//路面权重
        int jianxiudao = civilEnginneeringTechnicalTotal.getJianxiudaoLimit();//检修道权重
        int water = civilEnginneeringTechnicalTotal.getWaterLimit();//排水设施权重
        int marking = civilEnginneeringTechnicalTotal.getMarkingLimit();//交通标志或标线权重
        if(roadInfoService.checkByRoadName(civilEnginneeringTechnicalTotal.getRoadName())==null){
            return new AjaxResult().error("当前公路不存在，请输入正确的公路名称");
        }
        if ((direct + top + hole + roadsurface + jianxiudao + lining + water + marking + door)!= 100) {
            return new AjaxResult().error("各项权重和必须为100，请检查后重新输入");
        }
        return toAjax(civilEnginneeringTechnicalTotalService.updateCivilEnginneeringTechnicalTotal(civilEnginneeringTechnicalTotal));
    }

    /**
     * 删除土建结构技术状况评定
     */
    @PreAuthorize("@ss.hasPermi('civil:total:remove')")
    @Log(title = "土建结构技术状况评定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{technologyIds}")
    public AjaxResult remove(@PathVariable Long[] technologyIds)
    {
        return toAjax(civilEnginneeringTechnicalTotalService.deleteCivilEnginneeringTechnicalTotalByTechnologyIds(technologyIds));
    }
}
